package com.jd.charactercalculator.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import com.jd.charactercalculator.dao.CharacterDao;
import com.jd.charactercalculator.dto.param.CharacterParamDto;
import com.jd.charactercalculator.dto.param.UserParamDto;
import com.jd.charactercalculator.dto.result.CharacterResultDto;
import com.jd.charactercalculator.dto.result.PagingResultDto;
import com.jd.charactercalculator.dto.result.UserResultDto;
import com.jd.charactercalculator.entity.CharacterEntity;
import com.jd.charactercalculator.entity.UserEntity;
import com.jd.charactercalculator.service.CharacterService;
import com.jd.charactercalculator.util.ObjectUtils;

/**
 * 
 *
 * @author:孙林
 * @since:2012下午4:47:05
 * @version:1.0.0
 */
public class CharacterServiceImpl implements CharacterService {

	private CharacterDao characterDao;
	
	@Override
	public PagingResultDto<CharacterResultDto> getCharacters(CharacterParamDto characterParamDto) {
		//返回结果对象
		PagingResultDto<CharacterResultDto> pagingResultDto = new PagingResultDto<CharacterResultDto>();
		List<CharacterResultDto> characterResultDtoList = new ArrayList<CharacterResultDto>();

		//查询character表
		Map<String,Object> map =  ObjectUtils.objectToMap(characterParamDto);
		List<CharacterEntity> characterEntityList =  characterDao.getCharacters(map);

		//逻辑处理，将Entity转乘dto
		Iterator<CharacterEntity> iterator =  characterEntityList.iterator();
		while (iterator.hasNext()) {
			CharacterEntity characterEntity =  iterator.next();
			CharacterResultDto characterResultDto = new CharacterResultDto();
			BeanUtils.copyProperties(characterEntity, characterResultDto);
			characterResultDtoList.add(characterResultDto);
		}

		//取得所有人品数量
		int totalSize = characterDao.getTotalSize();
		
		//放入PagingResultDto中
		pagingResultDto.setList(characterResultDtoList);
		pagingResultDto.setTotalSize(totalSize);
		
		return pagingResultDto;
	}

	public CharacterDao getCharacterDao() {
		return characterDao;
	}

	public void setCharacterDao(CharacterDao characterDao) {
		this.characterDao = characterDao;
	}

}
